package com.wzh.bargain.service.impl;

import com.wzh.bargain.dao.BargainActivityDao;
import com.wzh.bargain.dao.OrderBargainDao;
import com.wzh.bargain.entity.BargainActivityEntity;
import com.wzh.bargain.entity.OrderBargainEntity;
import com.wzh.bargain.feign.OrderFeign;
import com.wzh.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzh.common.utils.PageUtils;
import com.wzh.common.utils.Query;

import com.wzh.bargain.dao.BarLogDao;
import com.wzh.bargain.entity.BarLogEntity;
import com.wzh.bargain.service.BarLogService;
import org.springframework.transaction.annotation.Transactional;


@Service("barLogService")
public class BarLogServiceImpl extends ServiceImpl<BarLogDao, BarLogEntity> implements BarLogService {

    @Autowired
    private BarLogDao barLogDao;

    @Autowired
    private BargainActivityDao bargainActivityDao;

    @Autowired
    private OrderBargainDao orderBargainDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String orderId = (String) params.get("orderId");
        IPage<BarLogEntity> page = this.page(
                new Query<BarLogEntity>().getPage(params),
                new QueryWrapper<BarLogEntity>().eq("order_id",orderId)
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public R bargaining(String userId,String userBid, String bargainId) {
        //查询活动
        OrderBargainEntity order=barLogDao.getOrderByUserIdAndBarId(userBid,bargainId);
        if (order!=null){
            BargainActivityEntity bargain = bargainActivityDao.selectById(bargainId);
            //判断活动时间
            Long startTime=bargain.getStartDate().getTime();
            Long endTime=bargain.getEndDate().getTime();
            Long nowTime=System.currentTimeMillis();
            if (nowTime<startTime){
                return R.error("活动未开始");
            }else if (nowTime>endTime){
                return R.error("活动已结束");
            }
            //判断该用户是否已砍价过
            Integer count=barLogDao.getLogCountByOrderIdAndUserId(order.getId().toString(),userId);
            if (count>0){
                return R.error("你已砍过价。。。。");
            }else {
                //判断砍价规则
                BigDecimal price=bargain.getGoodsPriced().subtract(bargain.getGoodsPrice());
                if (bargain.getRuleFlag()==0){
                    //判断砍价已经多少次
                    Integer num=barLogDao.getLogCountByBarIdAndBuserId(userBid,order.getId());
                    if (num>bargain.getRuleNum()){
                        return R.error("最多只能砍"+bargain.getRuleNum()+"刀");
                    }
                    price=price.divide(new BigDecimal(bargain.getRuleNum()), 2,BigDecimal.ROUND_UNNECESSARY);
                    order.setGoodsPrice(order.getGoodsPrice().subtract(price));
                }else if (bargain.getRuleFlag()==1){
                    price =new BigDecimal( new Random().nextInt(bargain.getRuleNum().intValue()));
                    //如果当前砍价小于你的最底价就默认砍到你的底价
                    if(order.getGoodsPrice().subtract(price).doubleValue()<bargain.getGoodsPrice().doubleValue()){
                        price=bargain.getGoodsPrice();
                    }
                    order.setGoodsPrice(order.getGoodsPrice().subtract(price));
                }
                //判断低价
                if(order.getGoodsPrice().doubleValue()<bargain.getGoodsPrice().doubleValue()){
                    return R.error("不得低于最低价");
                }
                //修改砍价订单表
                int i = orderBargainDao.updateById(order);
                if (i>0){
                    //添加log日志
                    BarLogEntity barLog = new BarLogEntity();
                    barLog.setOrderId(Math.toIntExact(order.getId()));
                    barLog.setBarDate(new Date());
                    barLog.setUserId(Integer.valueOf(userId));
                    barLog.setBuserId(Integer.valueOf(userBid));
                    barLog.setPrice(price);
                    baseMapper.insert(barLog);
                    return R.ok("砍价成功");
                }else {
                    return R.error("调用失败");
                }
            }
        }
        return R.error("无该砍价活动");
    }

}